//
//  setViewController.m
//  tcpoctest
//
//  Created by gump on 10/22/12.
//  Copyright (c) 2012 gump. All rights reserved.
//

#import "setViewController.h"
#import "WFLabel_blackShadow.h"
#import "netManager.h"
#import "ViewManager.h"
#import <QuartzCore/QuartzCore.h>
#import "FTWButton.h"
#import "LoginViewController.h"

#import "FirCirPersonDataSource.h"

#import "sendNewsLength.h"
#import "sendQuitLogin.h"

#import "UMFeedback.h"

#import "chatDataSource.h"

@interface setViewController ()

@end

@implementation setViewController

- (id)init
{
    self = [super init];
    if (self) {
        mtable = nil;
    }
    return self;
}

-(void)initUI
{
    self.view.backgroundColor = VIEW_BG_COLOR;
    
    [self createTitle];
    self.mtitle.text = @"设置";
    
    mtable =[[UITableView alloc]initWithFrame:CGRectMake(0, TOPBAR_HIGHT, WINDOW_WIDTH, WINDOW_HIGTH - TOPBAR_HIGHT - 20)
                                                     style:UITableViewStyleGrouped];
    mtable.backgroundColor=CELL_BG_COLOR;
    mtable.separatorStyle=UITableViewCellSeparatorStyleNone;
    mtable.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"bgColor.png"]];
    mtable.backgroundView = nil;
    mtable.delegate =self;
    mtable.dataSource =self;
    [self.view addSubview:mtable];
    
    FTWButton *logout = [[FTWButton alloc] init];
    logout.frame = CGRectMake(7, 310, 306, 44);
    [logout addDeleteStyleForState:UIControlStateNormal];
    [logout setText:@"退出登陆" forControlState:UIControlStateNormal];
    [logout setFont:[UIFont systemFontOfSize:18]];
    [logout addTarget:self action:@selector(logoutButtonPress) forControlEvents:UIControlEventTouchUpInside];
    [mtable addSubview:logout];
}

-(void)logoutButtonPress
{
    
    [[netManager sharedNetManager].sendquitl SendQuitLogin];
    [DataControl shareControl].blogin = false;
    [DataControl shareControl].bfirstlogin = true;
    
    setLocalMail(@"");
    
    [[DataControl shareControl].chatDS RemoveAllChatDic];
    [DataControl shareControl].chatDS.currentChatArr = nil;
    
    [[DataControl shareControl] clearData];
    
    [[ViewManager sharedViewManager].ShowViewArr removeAllObjects];
    
    NSString *strppnum = [NSString stringWithFormat:@"%d",getLocalPP()];
    if (![strppnum isEqualToString:@"-1"]) {
        [[ViewManager sharedViewManager].loginvc SetMail:strppnum];
    }
    
    [[ViewManager sharedViewManager] showView:VI_LOGIN
                             withIsRemoveCurV:true
                                withDirection:VD_RIGHT];
    
    
}

#pragma mark - tableView DataSource -
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 4;
}

-(float)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 44;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    switch (section) {
        case 0:
            return 1;
            break;
        case 1:
            return 1;
            break;
        case 2:
            return 1;
            break;
//        case 3:
//            return 1;
//            break;
        default:
            break;
    }
    return 0;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSUInteger section=[indexPath section];
    static NSString *CellIdentifier = @"Cell";
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] ;
		cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.backgroundColor = [UIColor clearColor];
            [cell setBackgroundView:[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"cell.png"]]];
            UIImageView *selectImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"cellpress.png"]];
            [cell setSelectedBackgroundView:selectImageView];
    }
    switch (section)
    {
        case 0:
        {
            switch (indexPath.row) {
                case 0:
                {
                    cell.selectionStyle=UITableViewCellSelectionStyleBlue;
                    cell.textLabel.text = @"个人资料";
                    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
                    
                    CGRect headFrame =  CGRectMake(230, 3, 38, 38);
                    UIImageView *headImgView = [[UIImageView alloc] initWithFrame:headFrame];
                    headImgView.layer.cornerRadius = 5;
                    headImgView.layer.masksToBounds = YES;
                    headImgView.userInteractionEnabled = NO;
                    NSString *strurl = [[DataControl shareControl] SearchHeadImage:
                                        [DataControl shareControl].personDS.uinfo.mail];
                    if (![strurl isEqual: @""])
                    {
                        headImgView.image = [UIImage imageWithContentsOfFile:strurl];
                    }
                    [cell.backgroundView addSubview:headImgView];
                }
                    break;
                default:
                    break;
            }
            
            
        }
            break;
            
//            case 1:
//        {
//            cell.selectionStyle=UITableViewCellSelectionStyleBlue;
//            cell.textLabel.text = @"关于碰碰圈";
//            cell.accessoryType = UITableViewCellAccessoryNone;
//        }
//            break;
        case 1:
        {
            cell.selectionStyle=UITableViewCellSelectionStyleBlue;
            cell.textLabel.text = @"意见反馈";
            cell.accessoryType = UITableViewCellAccessoryNone;
            
        }
            break;
            
        case 2:
        {
            cell.selectionStyle=UITableViewCellSelectionStyleBlue;
            cell.textLabel.text = @"清除缓存";
            cell.accessoryType = UITableViewCellAccessoryNone;
        }
            break;
            
        default:
        break;
    }
    
    return cell;
}

#pragma mark -table delegate-
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    if ([indexPath section] == 0) {
        switch (indexPath.row) {
            case 0:
            {
                [[ViewManager sharedViewManager] showView:VI_USERINFO
                                         withIsRemoveCurV:false withDirection:VD_RIGHT];
            }
                break;
            default:
                break;
        }
        
    }
//    else if([indexPath section] == 1)
//    {
//        [[ViewManager sharedViewManager] showView:VI_HELP_WEB
//                                 withIsRemoveCurV:false withDirection:VD_RIGHT];
//    }
    else if([indexPath section] == 1)
    {
        [UMFeedback showFeedback:self withAppkey:T_UMENG];
    }else if([indexPath section] == 2)
    {
        [[DataControl shareControl].LCDS addLogicCommand:LC_DELETECACHE];
        
        [[ViewManager sharedViewManager] showSuccessProgressHUDWithStatus:@"正在清理"
                                                                 withView:self.view];
    }
}

#pragma mark -private fun-
-(void)refreshView
{
    [mtable reloadData];
}

-(void)viewWillShow
{
    [mtable reloadData];
}

-(void)deleteCacheOver
{
    [[ViewManager sharedViewManager] ChangeHUDShowSuccess:@"清理完成"];
}

@end
